<?php
class ProjectFlagsController extends AppController
{
    public $name = 'ProjectFlags';
    public function beforeFilter()
    {
        if (!Configure::read('Project.is_project_flag_enabled')) {
            throw new NotFoundException(__l('Invalid request'));
        }
        parent::beforeFilter();
    }
    public function add($project_id = null)
    {
        $this->pageTitle = __l('Add Project Flag');
        if (!empty($this->request->data)) {
            $this->ProjectFlag->create();
            if ($this->Auth->user('user_type_id') != ConstUserTypes::Admin) {
                $this->request->data['ProjectFlag']['user_id'] = $this->Auth->user('id');
            }
            $this->request->data['ProjectFlag']['project_id'] = $this->request->data['Project']['id'];
			$this->request->data['ProjectFlag']['ip_id'] = $this->ProjectFlag->toSaveIp();
            if ($this->ProjectFlag->save($this->request->data)) {
                $this->Session->setFlash(__l('Project Flag has been added') , 'default', null, 'success');
                $project = $this->ProjectFlag->Project->find('first', array(
                    'conditions' => array(
                        'Project.id' => $this->request->data['Project']['id'],
                    ) ,
                    'fields' => array(
                        'Project.slug',
                    ) ,
                    'recursive' => - 1
                ));
                if ($this->RequestHandler->isAjax()) {
                    echo "success";
                    exit;
                } else {
                    $this->redirect(array(
                        'controller' => 'Projects',
                        'action' => 'view',
                        $project['Project']['slug'],
                        'admin' => false
                    ));
                }
            } else {
                $this->request->data = $this->ProjectFlag->Project->find('first', array(
                    'conditions' => array(
                        'Project.id' => $this->request->data['Project']['id'],
                    ) ,
                    'recursive' => 1
                ));
                $this->Session->setFlash(sprintf(__l('Flag could not be added. Please, try again')) , 'default', null, 'error');
            }
        } else {
            $this->request->data = $this->ProjectFlag->Project->find('first', array(
                'conditions' => array(
                    'Project.id' => $project_id,
                ) ,
                'recursive' => - 1
            ));
            if (empty($this->request->data)) {
                throw new NotFoundException(__l('Invalid request'));
            }
        }
        $projectFlagCategories = $this->ProjectFlag->ProjectFlagCategory->find('list', array(
            'conditions' => array(
                'ProjectFlagCategory.is_active' => 1
            )
        ));
        if ($this->Auth->user('user_type_id') == ConstUserTypes::Admin) {
            $users = $this->ProjectFlag->User->find('list');
            $this->set(compact('users'));
        }
        $projects = $this->ProjectFlag->Project->find('list');
        $this->set(compact('users', 'projects', 'projectFlagCategories'));
    }
    public function admin_index()
    {
        $this->pageTitle = __l('Project Flags');
        $this->ProjectFlag->recursive = 0;
        $conditions = array();
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'day') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(ProjectFlag.created) <= '] = 0;
            $this->pageTitle.= __l(' - Flagged today');
        }
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'week') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(ProjectFlag.created) <= '] = 7;
            $this->pageTitle.= __l(' - Flagged in this week');
        }
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'month') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(ProjectFlag.created) <= '] = 30;
            $this->pageTitle.= __l(' - Flagged in this month');
        }
        if (!empty($this->request->params['named']['project_id'])) {
            $conditions['ProjectFlag.project_id'] = $this->request->params['named']['project_id'];
        }
        if (!empty($this->request->params['named']['user_id'])) {
            $conditions['ProjectFlag.user_id'] = $this->request->params['named']['user_id'];
        }
        // condition for elememt in user view page
        if (!empty($this->request->params['named']['view_type']) && $this->request->params['named']['view_type'] == 'user_view') {
            if (!empty($this->request->params['named']['project_id'])) {
                $conditions['ProjectFlag.project_id'] = $this->request->params['named']['project_id'];
            }
        }
		if (!empty($this->request->params['named']['project_id'])) {
                $conditions['ProjectFlag.project_id'] = $this->request->params['named']['project_id'];
				$project_name = $this->ProjectFlag->Project->find('first', array(
                'conditions' => array(
                    'Project.id' => $this->request->params['named']['project_id'],
                ) ,
                'fields' => array(
                    'Project.name',
                ) ,
                'recursive' => - 1,
            ));
            $this->pageTitle.= sprintf(__l(' - %s') , $project_name['Project']['name']);
            }
        $this->paginate = array(
            'conditions' => $conditions,
            'contain' => array(
                'Project' => array(
                    'ProjectStatus',
                ) ,
                'User' => array(
                    'fields' => array(
                        'User.username'
                    )
                ) ,
                'ProjectFlagCategory' => array(
                    'fields' => array(
                        'ProjectFlagCategory.name'
                    )
                ) ,
				'Ip' => array(
                    'City' => array(
                        'fields' => array(
                            'City.name',
                        )
                    ) ,
                    'State' => array(
                        'fields' => array(
                            'State.name',
                        )
                    ) ,
                    'Country' => array(
                        'fields' => array(
                            'Country.name',
                            'Country.iso2',
                        )
                    ) ,
                    'fields' => array(
                        'Ip.ip',
                        'Ip.latitude',
                        'Ip.longitude',
						'Ip.host'
                    )
                ) , 
            ) ,
            'order' => array(
                'ProjectFlag.id' => 'desc'
            ) ,
            'recursive' => 0
        );
        $this->set('projectFlags', $this->paginate());
		$moreActions = $this->ProjectFlag->moreActions;
		$this->set(compact('moreActions'));
    }
    public function admin_delete($id = null)
    {
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if ($this->ProjectFlag->delete($id)) {
            $this->Session->setFlash(__l('Project Flag deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            throw new NotFoundException(__l('Invalid request'));
        }
    }
	public function admin_update()
    {
        if (!empty($this->request->data['ProjectFlag'])) {
            $r = $this->request->data[$this->modelClass]['r'];
            $actionid = $this->request->data[$this->modelClass]['more_action_id'];
            unset($this->request->data[$this->modelClass]['r']);
            unset($this->request->data[$this->modelClass]['more_action_id']);
            $userIds = array();
            foreach($this->request->data['ProjectFlag'] as $user_id => $is_checked) {
                if ($is_checked['id']) {
                    $userIds[] = $user_id;
                }
            }
            if ($actionid && !empty($userIds)) {
                   if ($actionid == ConstMoreAction::Delete) {
                    $this->ProjectFlag->deleteAll(array(
                        'ProjectFlag.id' => $userIds
                    ));
                    $this->Session->setFlash(__l('Checked Project flag has been deleted') , 'default', null, 'success');
                }
            }
        }
        $this->redirect(Router::url('/', true) . $r);
    }
}
?>